﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.TracebackCommand;
using Traceability.Domain;
using Traceability.Domain.DTO.Output;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.TracebackCommandHandler
{
    public class TracebackFirstCommandHandler : IRequestHandler<TracebackFirstCommand, APIResult<TracebackFirstDTO>>
    {
        private readonly IBaseRepository<Traceback_Information> _tracebackRepository;
        private readonly IMapper mapper;

        public TracebackFirstCommandHandler(IBaseRepository<Traceback_Information> baseRepository, IMapper mapper)
        {
            this._tracebackRepository = baseRepository;
            this.mapper = mapper;
        }

        public Task<APIResult<TracebackFirstDTO>> Handle(TracebackFirstCommand request, CancellationToken cancellationToken)
        {
            var res = _tracebackRepository.GetAll().AsNoTracking().FirstOrDefault(s => s.Id == request.Id);
            var model = mapper.Map<TracebackFirstDTO>(res);
            APIResult<TracebackFirstDTO> aPIResult = new APIResult<TracebackFirstDTO>()
            {
                Code = EnumCode.Ok,
                Msg = "显示成功",
                Data = model
            };
            return Task.FromResult(aPIResult);

        }
    }
}
